package com.aop.additional_function;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Author: tgy
 * @Date: 2020-08-07 13:42
 */

class Dog{

    public void run(){

        System.out.println("dog run");
    }

}

public class CGLibProxy {

    public static void main(String[] args) {


        Dog dog = new Dog();

        Enhancer enhancer = new Enhancer();

        enhancer.setSuperclass(Dog.class);

        enhancer.setClassLoader(CGLibProxy.class.getClassLoader());

        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {


                System.out.println("run before");
                Object result = method.invoke(dog, objects);
                System.out.println("run after");
                return result;
            }
        });

        Dog dogProxy =(Dog) enhancer.create();

        System.out.println(dogProxy);
        dogProxy.run();
//        Object o = enhancer.create();

    }
}
